package org.chunchu.todo.dao;

import java.util.ArrayList;
import java.util.List;

import javax.inject.Inject;

import org.chunchu.todo.LogUtils;
import org.chunchu.todo.model.Task;

import android.content.ContentValues;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.provider.BaseColumns;

public class TaskDao {

	public static final String T_TASK = "t_task";

	public static final String F_NAME = "name";
	public static final String F_DETAIL = "detail";
	public static final String F_CATEGORY_ID = "category_id";

	public static final String[] ALL_COLUMNS = { BaseColumns._ID, F_NAME,
			F_DETAIL, F_CATEGORY_ID };

	@Inject
	DatabaseOpenHelper helper;

	public TaskDao() {
	}

	public List<Task> findAll() {
		LogUtils.d("Task.findAll");
		SQLiteDatabase database = helper.getDatabase();
		Cursor cursor = database.query(T_TASK, ALL_COLUMNS, null, null, null,
				null, null);
		LogUtils.i(cursor.getCount(), " task found ");
		List<Task> tasks = new ArrayList<Task>(cursor.getCount());
		while (cursor.moveToNext()) {
			Task task = new Task(cursor.getLong(0), cursor.getString(1),
					cursor.getString(2), cursor.getLong(3));
			tasks.add(task);
		}
		return tasks;
	}

	public List<Task> findByCategoryId(long id) {
		LogUtils.d("Task.findByCategoryId", id);
		SQLiteDatabase database = helper.getDatabase();
		Cursor cursor = null;
		if (id == 1) {
			LogUtils.d("Category is All Task");
			cursor = database.query(T_TASK, ALL_COLUMNS, null, null, null,
					null, null);
		} else {
			cursor = database.query(T_TASK, ALL_COLUMNS, F_CATEGORY_ID
					+ " = ? ", new String[] { String.valueOf(id) }, null, null,
					null);
		}
		LogUtils.i(cursor.getCount(), " tasks found ");
		List<Task> tasks = new ArrayList<Task>(cursor.getCount());
		while (cursor.moveToNext()) {
			Task task = new Task(cursor.getLong(0), cursor.getString(1),
					cursor.getString(2), cursor.getLong(3));
			tasks.add(task);
		}
		return tasks;
	}

	public Task findById(long id) {
		LogUtils.d("Task.findById", id);
		Task newTask = null;
		SQLiteDatabase database = helper.getDatabase();
		Cursor cursor = database.query(T_TASK, ALL_COLUMNS, BaseColumns._ID
				+ " = ? ", new String[] { String.valueOf(id) }, null, null,
				null);
		if (cursor.moveToFirst()) {
			newTask = new Task(cursor.getLong(0), cursor.getString(1),
					cursor.getString(2), cursor.getLong(3));
		}
		return newTask;

	}

	public void remove(long id) {
		LogUtils.d("Task.remove", id);
		SQLiteDatabase database = helper.getDatabase();
		database.delete(T_TASK, BaseColumns._ID + " = ?",
				new String[] { String.valueOf(id) });
	}

	public void removeByCategoryId(long id) {
		LogUtils.d("Task.removeByCategoryId", id);
		SQLiteDatabase database = helper.getDatabase();
		database.delete(T_TASK, F_CATEGORY_ID + " = ?",
				new String[] { String.valueOf(id) });
	}

	public boolean isValid(Task editTask) {
		boolean value = false;

		if (editTask == null) {
			LogUtils.w("Task.isValid with editTask = null");
			return false;
		}
		value = isValidString(editTask.getName())
				&& isValidString(editTask.getDetail())
				&& editTask.getCategoryId() > 0;
		LogUtils.i("Task [", editTask.getName(), " | ", editTask.getDetail(),
				"].isValid", value);
		return value;
	}

	private boolean isValidString(String value) {
		boolean val = value != null && value.trim().length() > 0;
		LogUtils.i("String [", value, "].isValid", val);
		return val;

	}

	public void add(Task editTask) {
		LogUtils.d("Task.add", editTask.getName());
		SQLiteDatabase database = helper.getDatabase();
		helper.addTask(database, editTask.getCategoryId(), editTask.getName(),
				editTask.getDetail());
	}

	public void update(Task editTask) {
		LogUtils.d("Task.update", editTask.getName());
		SQLiteDatabase database = helper.getDatabase();
		ContentValues values = new ContentValues();
		values.put(F_CATEGORY_ID, editTask.getCategoryId());
		values.put(F_NAME, editTask.getName());
		values.put(F_DETAIL, editTask.getDetail());
		database.update(T_TASK, values, BaseColumns._ID + " = ? ",
				new String[] { String.valueOf(editTask.getId()) });
	}

}
